package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.qianniu.napi.admin.entity.*;
import com.qianniu.napi.admin.entity.penum.XftypeEnum;
import com.qianniu.napi.admin.mapper.LeagueMapper;
import com.qianniu.napi.admin.mapper.StoreStatMapper;
import com.qianniu.napi.admin.service.*;
import com.qianniu.napi.common.util.DateUtil;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 商家账务表
 * </p>
 *
 * @author chenxuebin
 * @since 2018-08-08
 */
@RestController
public class SaccountController {

    private static final Logger logger = LoggerFactory.getLogger(SaccountController.class);

    @Autowired
    private ISaccountService saccountService;
    @Autowired
    private ISaccountFlowService saccountFlowService;
    @Autowired
    private IRedisService redisService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IJifenService jifenService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IAccountService accountService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IOrderStatService orderStatService;
    @Autowired
    private IChoujiangLogService choujiangLogService;
    @Autowired
    private IChoujiangService choujiangService;
    @Autowired
    private IOrderGoodsService orderGoodsService;
    @Autowired
    private ICouponTplService couponTplService;
    @Autowired
    private IStoreService storeService;
    @Autowired
    private StoreStatMapper storeStatMapper;
    @Autowired
    private LeagueMapper leagueMapper;
    @Autowired
    private IHexiaoLogService hexiaoLogService;

    @RequestMapping("/api/mch/caiwugk")
    @ResponseBody
    public Jinfo caiwugk(@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                         @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        Jinfo info = new Jinfo();
        Long sid = ObjectUtil.getSid();
        if (sid == null) {
            info.setCode(1);
            info.setMsg("无权限操作");
            return info;
        }

//        Store store = storeService.selectOne(new EntityWrapper<Store>()
//                .setSqlSelect("id,name,jianchen,pic,address,latitude,longitude,phone,wxno")
//                .eq("id", sid));
//        info.setStore(store);

        Saccount saccount = saccountService.selectById(sid);
        if (saccount == null) {
            info.setCode(1);
            info.setMsg("账户不存在");
            return info;
        }

        Map map = new HashMap<String, Object>();
        Map tjmap = new HashMap<String, Object>();
        map.put("xiaoshou", saccount.getSaleAmount());//总销售额|总订单额
        map.put("shishou", saccount.getAmount());
        map.put("yingxiao", saccount.getMamount());
        map.put("youhui", saccount.getYouhui());
        map.put("tuikuan", saccount.getRefund());

        //买单统计
        SaccountFlow saccountFlow = saccountFlowService.selectOne(new EntityWrapper<SaccountFlow>()
                .setSqlSelect("sid,sum(amount) amount,sum(total) total,sum(youhui) youhui,sum(mamount) mamount,sum(1) jifen")
                .eq("sid", sid)
                .eq("xftype", XftypeEnum.smaidan.getVal())
                .groupBy("sid")
        );
        if (saccountFlow != null) {
            Order smaidanOrder = orderService.selectOne(new EntityWrapper<Order>()
                    .setSqlSelect("sid,sum(realpay) realpay")
                    .eq("sid", sid).ge("status", 2).eq("is_refund", 1)
                    .eq("xftype", XftypeEnum.smaidan.getVal())
                    .groupBy("sid")
            );

            Map maidan = new HashMap<String, Object>();
            maidan.put("orders", saccountFlow.getJifen());
            maidan.put("shishou", saccountFlow.getAmount());
            maidan.put("xiaoshou", saccountFlow.getTotal());
            maidan.put("yingxiao", saccountFlow.getMamount());
            maidan.put("youhui", saccountFlow.getYouhui());
            maidan.put("tuikuan", smaidanOrder == null ? 0 : smaidanOrder.getRealpay());

            tjmap.put("maidan",maidan);
        }

        //商品统计
        SaccountFlow spsaccountFlow = saccountFlowService.selectOne(new EntityWrapper<SaccountFlow>()
                .setSqlSelect("sid,sum(amount) amount,sum(total) total,sum(youhui) youhui,sum(mamount) mamount,sum(1) jifen")
                .eq("sid", sid)
                .eq("xftype", XftypeEnum.sell.getVal())
                .eq("type", 0)
                .groupBy("sid")
        );
        if (spsaccountFlow != null) {
            Order spOrder = orderService.selectOne(new EntityWrapper<Order>()
                    .setSqlSelect("sid,sum(realpay) realpay")
                    .eq("sid", sid).ge("status", 2).eq("is_refund", 1)
                    .eq("xftype", XftypeEnum.buy.getVal())
                    .groupBy("sid")
            );

            Map sporders = new HashMap<String, Object>();
            sporders.put("orders", spsaccountFlow.getJifen());
            sporders.put("shishou", spsaccountFlow.getAmount());
            sporders.put("xiaoshou", spsaccountFlow.getTotal());
            sporders.put("yingxiao", spsaccountFlow.getMamount());
            sporders.put("youhui", spsaccountFlow.getYouhui());
            sporders.put("tuikuan", spOrder == null ? 0 : spOrder.getRealpay());

            tjmap.put("sporders",sporders);
        }

        //活动统计
        SaccountFlow hdsaccountFlow = saccountFlowService.selectOne(new EntityWrapper<SaccountFlow>()
                .setSqlSelect("sid,sum(amount) amount,sum(total) total,sum(youhui) youhui,sum(mamount) mamount,sum(1) jifen")
                .eq("sid", sid)
                .eq("xftype", XftypeEnum.sell.getVal())
                .gt("type", 0)
                .groupBy("sid")
        );
        if (hdsaccountFlow != null) {
            Order hdOrder = orderService.selectOne(new EntityWrapper<Order>()
                    .setSqlSelect("sid,sum(realpay) realpay")
                    .eq("sid", sid).ge("status", 2).eq("is_refund", 1)
                    .eq("xftype", XftypeEnum.buy.getVal())
                    .groupBy("sid")
            );

            Map hdorders = new HashMap<String, Object>();
            hdorders.put("orders", hdsaccountFlow.getJifen());
            hdorders.put("shishou", hdsaccountFlow.getAmount());
            hdorders.put("xiaoshou", hdsaccountFlow.getTotal());
            hdorders.put("yingxiao", hdsaccountFlow.getMamount());
            hdorders.put("youhui", hdsaccountFlow.getYouhui());
            hdorders.put("tuikuan", hdOrder == null ? 0 : hdOrder.getRealpay());

            tjmap.put("hdorders",hdorders);
        }

        //菜品统计

        //充值统计

        info.setInfo(map);
        info.setExtra(tjmap);

        return info;
    }

    //联盟财务概况《联盟端》
    @RequestMapping("/api/mch/lmcaiwugk")
    @ResponseBody
    public Jinfo lmcaiwugk() {
        Jinfo info = new Jinfo();
        Long leagueid = redisService.getLeagueid();
        if (leagueid == null) {
            info.setCode(1);
            info.setMsg("无权限操作");
            return info;
        }

//        Store store = storeService.selectOne(new EntityWrapper<Store>()
//                .setSqlSelect("id,name,jianchen,pic,address,latitude,longitude,phone,wxno")
//                .eq("id", sid));
//        info.setStore(store);

        Saccount saccount = saccountService.selectOne(new EntityWrapper<Saccount>()
                .setSqlSelect("leagueid,sum(sale_amount) saleAmount,sum(amount) amount,sum(mamount) mamount" +
                        ",sum(youhui) youhui,sum(refund) refund")
                .eq("leagueid", leagueid)
                .groupBy("leagueid"));

        if (saccount == null) {
            info.setCode(1);
            info.setMsg("账户不存在");
            return info;
        }

        Map map = new HashMap<String, Object>();
        map.put("xiaoshou", saccount.getSaleAmount());//总销售额|总订单额
        map.put("shishou", saccount.getAmount());
        map.put("yingxiao", saccount.getMamount());
        map.put("youhui", saccount.getYouhui());
        map.put("tuikuan", saccount.getRefund());

        //买单统计
        SaccountFlow saccountFlow = saccountFlowService.selectOne(new EntityWrapper<SaccountFlow>()
                .setSqlSelect("sid,sum(amount) amount,sum(total) total,sum(youhui) youhui,sum(mamount) mamount,sum(1) jifen")
                .eq("leagueid", leagueid)
                .eq("xftype", XftypeEnum.smaidan.getVal())
                .groupBy("sid")
        );
        if (saccountFlow != null) {
            Order smaidanOrder = orderService.selectOne(new EntityWrapper<Order>()
                    .setSqlSelect("sid,sum(realpay) realpay")
                    .eq("leagueid", leagueid).ge("status", 2).eq("is_refund", 1)
                    .eq("xftype", XftypeEnum.smaidan.getVal())
                    .groupBy("sid")
            );

            Map maidan = new HashMap<String, Object>();
            maidan.put("orders", saccountFlow.getJifen());
            maidan.put("shishou", saccountFlow.getAmount());
            maidan.put("xiaoshou", saccountFlow.getTotal());
            maidan.put("yingxiao", saccountFlow.getMamount());
            maidan.put("youhui", saccountFlow.getYouhui());
            maidan.put("tuikuan", smaidanOrder == null ? 0 : smaidanOrder.getRealpay());

            info.setExtra(maidan);
        }

        //商品统计
//        SaccountFlow saccountFlow = saccountFlowService.selectOne(new EntityWrapper<SaccountFlow>()
//                .setSqlSelect("sid,sum(amount) amount,sum(total) total,sum(youhui) youhui,sum(mamount) mamount,sum(1) jifen")
//                .eq("sid", sid).ge("status", 2)
//                .eq("xftype", XftypeEnum.smaidan.getName())
//                .groupBy("sid")
//        );
//        Order smaidanOrder = orderService.selectOne(new EntityWrapper<Order>()
//                .setSqlSelect("sid,sum(realpay) realpay")
//                .eq("sid", sid).ge("status", 2).eq("is_refund", 1)
//                .eq("xftype", XftypeEnum.smaidan.getName())
//                .groupBy("sid")
//        );
//        JSONObject maidan = new JSONObject();
//        maidan.put("orders",saccountFlow.getJifen());
//        maidan.put("shishou",saccountFlow.getAmount());
//        maidan.put("xiaoshou",saccountFlow.getTotal());
//        maidan.put("yingxiao",saccountFlow.getMamount());
//        maidan.put("youhui",saccountFlow.getYouhui());
//        maidan.put("tuikuan",smaidanOrder.getRealpay());
//        map.put("maidan", maidan);


        //活动统计

        //菜品统计

        //充值统计

        info.setInfo(map);

        return info;
    }


    //营销版商家概况中心
    @RequestMapping("/api/mch/statcenter")
    @ResponseBody
    public Jinfo mchstatcenter() {

        Jinfo json = new Jinfo();
        Long sid = ObjectUtil.getSid();
        if (sid == null || sid == 0) {
            json.setCode(1);
            json.setMsg("没有权限查看商家概况");
            return json;
        }
        Store store = storeService.selectById(sid);
        if (sid == null || sid == 0 || store.getState() != 1) {
            json.setCode(1);
            json.setMsg("商家不存在或商家已下线");
            return json;
        }

//        User muser = userService.selectOne(new EntityWrapper<User>()
//                .eq("is_mch", 1).eq("is_admin", 1).eq("sid", sid));
//        if (muser == null) {
//            json.setCode(1);
//            json.setMsg("商家管理员不存在");
//            return json;
//        }

        Saccount saccount = saccountService.selectOne(new EntityWrapper<Saccount>().eq("sid", sid));
        if (saccount == null) {
            saccount = saccountService.initSaccount(ObjectUtil.getMid(), ObjectUtil.getMpid(), sid, store.getLeagueid());
//            json.setCode(1);
//            json.setMsg("商家资金账户不存在");
//            return json;
        }
        BigDecimal zero = new BigDecimal(0);

        //统计订单数包含有效订单+无效订单,故status>=1
        Order ucout = orderService.selectOne(new EntityWrapper<Order>()
                .setSqlSelect("count(distinct uid) goodsNumber")
                .eq("sid", sid).ge("status", 1));

        //统计订单数包含有效订单+无效订单,故status>=1
        int ddshu = orderService.selectCount(new EntityWrapper<Order>()
                .eq("sid", sid).ge("status", 1));
        int yxddshu = orderService.selectCount(new EntityWrapper<Order>()
                .eq("sid", sid).ge("status", 2));

        int shangpinshu = goodsService.selectCount(new EntityWrapper<Goods>()
                .eq("sid", sid).eq("type", 0));

        int huodongshu = goodsService.selectCount(new EntityWrapper<Goods>()
                .eq("sid", sid).gt("type", 0));

        //===待完善***
        StoreStat storeStat = storeStatMapper.selectById(sid);
        storeStat.setKeyong(saccount.getAmount());
        storeStat.setDairuzhang(saccount.getFanliing());
        storeStat.setUsers(ucout == null||ucout.getGoodsNumber()== 0? 0 : ucout.getGoodsNumber());
        storeStat.setOrders(ddshu);
        storeStat.setYxorders(yxddshu);
        storeStat.setHuodongs(huodongshu);
        storeStat.setGoods(shangpinshu);

        Map<String, SaccountFlow> stat = new HashMap<String, SaccountFlow>();
        //===今日统计
        int today = Integer.parseInt(DateUtil.getToday("YYYYMMdd"));
        SaccountFlow tianstat = saccountFlowService.selectOne(new EntityWrapper<SaccountFlow>()
                .setSqlSelect("sid,sum(amount) amount,sum(total) total,sum(youhui) youhui,sum(mamount) mamount,sum(mamount) mamount,sum(1) jifen")
                .eq("sid", sid)
                .eq("createday", today)
                .ne("xftype", XftypeEnum.ruzhang.getVal())
                .ne("xftype", XftypeEnum.refund.getVal())
                .groupBy("sid")
        );
        //===待完善***退款流水待完善
        SaccountFlow tiantui = saccountFlowService.selectOne(new EntityWrapper<SaccountFlow>()
                .setSqlSelect("sum(amount) amount")
                .eq("sid", sid)
                .eq("createday", today)
                .eq("xftype", XftypeEnum.refund.getVal())
                .groupBy("sid")
        );
        int tiandd = saccountFlowService.selectCount(new EntityWrapper<SaccountFlow>()
                .setSqlSelect("1")
                .eq("sid", sid)
                .eq("iotype", 1)
                .eq("createday", today)
                .ne("xftype", XftypeEnum.ruzhang.getVal())
        );
        if (tianstat == null) tianstat = new SaccountFlow();
        tianstat.setTuikuan(tiantui == null ? zero : tiantui.getAmount());
        tianstat.setOrders(tiandd);
        stat.put("tian", tianstat);

        //===周统计
        int zhou0 = Integer.parseInt(DateUtil.addToday(-6, "YYYYMMdd"));
        int zhou1 = Integer.parseInt(DateUtil.getToday("YYYYMMdd"));
        SaccountFlow zhoustat = saccountFlowService.selectOne(new EntityWrapper<SaccountFlow>()
                .setSqlSelect("sid,sum(amount) amount,sum(total) total,sum(youhui) youhui,sum(mamount) mamount,sum(mamount) mamount,sum(1) jifen")
                .eq("sid", sid)
                .ge("createday", zhou0)
                .le("createday", zhou1)
                .ne("xftype", XftypeEnum.ruzhang.getVal())
                .ne("xftype", XftypeEnum.refund.getVal())
                .groupBy("sid")
        );

        SaccountFlow zhoutui = saccountFlowService.selectOne(new EntityWrapper<SaccountFlow>()
                .setSqlSelect("sum(amount) amount")
                .eq("sid", sid)
                .ge("createday", zhou0)
                .le("createday", zhou1)
                .eq("xftype", XftypeEnum.refund.getVal())
        );
        int zhoudd = saccountFlowService.selectCount(new EntityWrapper<SaccountFlow>()
                .setSqlSelect("1")
                .eq("sid", sid)
                .eq("iotype", 1)
                .ge("createday", zhou0)
                .le("createday", zhou1)
                .ne("xftype", XftypeEnum.ruzhang.getVal())
        );
        if (zhoustat == null) zhoustat = new SaccountFlow();
        zhoustat.setTuikuan(zhoutui == null ? zero : zhoutui.getAmount());
        zhoustat.setOrders(zhoudd);
        stat.put("zhou", zhoustat);

        //===月统计
        int yue0 = Integer.parseInt(DateUtil.addToday(-29, "YYYYMMdd"));
        int yue1 = Integer.parseInt(DateUtil.getToday("YYYYMMdd"));
        SaccountFlow yuestat = saccountFlowService.selectOne(new EntityWrapper<SaccountFlow>()
                .setSqlSelect("sid,sum(amount) amount,sum(total) total,sum(youhui) youhui,sum(mamount) mamount,sum(mamount) mamount,sum(1) jifen")
                .eq("sid", sid)
                .ge("createday", yue0)
                .le("createday", yue1)
                .ne("xftype", XftypeEnum.ruzhang.getVal())
                .ne("xftype", XftypeEnum.refund.getVal())
                .groupBy("sid")
        );
        SaccountFlow yuetui = saccountFlowService.selectOne(new EntityWrapper<SaccountFlow>()
                .setSqlSelect("sum(amount) amount")
                .eq("sid", sid)
                .ge("createday", yue0)
                .le("createday", yue1)
                .eq("xftype", XftypeEnum.refund.getVal())
        );
        int yuedd = saccountFlowService.selectCount(new EntityWrapper<SaccountFlow>()
                .setSqlSelect("1")
                .eq("sid", sid)
                .eq("iotype", 1)
                .ge("createday", zhou0)
                .le("createday", zhou1)
                .ne("xftype", XftypeEnum.ruzhang.getVal())
        );
        if (yuestat == null) yuestat = new SaccountFlow();
        yuestat.setTuikuan(yuetui == null ? zero : yuetui.getAmount());
        yuestat.setOrders(yuedd);
        stat.put("yue", yuestat);

        json.setInfo(storeStat);
        json.setExtra(stat);

        return json;
    }


    //联盟版盟主概况中心
    @RequestMapping("/api/agent/statcenter")
    @ResponseBody
    public Jinfo agentstatcenter() {

        Jinfo json = new Jinfo();
        Long uid = ObjectUtil.getUid();
        if (uid == null || uid == 0) {
            json.setCode(1);
            json.setMsg("请授权登录之后再操作");
            return json;
        }
        Long leagueid = redisService.getUser().getLeagueid();
        if(leagueid==null) {
            json.setCode(1);
            json.setMsg("当前用户非盟主无权查看");
            return json;
        }

        League league = leagueMapper.selectById(leagueid);

        if (league == null) {
            json.setCode(1);
            json.setMsg("联盟商圈信息不存在");
            return json;
        }

        //商家统计
        Saccount saccount = saccountService.selectOne(new EntityWrapper<Saccount>()
                .setSqlSelect("sum(amount) amount,sum(fanliing) fanliing")
                .eq("leagueid", leagueid));

        BigDecimal zero = new BigDecimal(0);
        Order ucout = orderService.selectOne(new EntityWrapper<Order>()
                .setSqlSelect("count(distinct uid) goodsNumber")
                .eq("leagueid", leagueid).ge("status", 2));
        int ddshu = orderService.selectCount(new EntityWrapper<Order>()
                .eq("leagueid", leagueid).ge("status", 2));

        int shangpinshu = goodsService.selectCount(new EntityWrapper<Goods>()
                .eq("leagueid", leagueid).eq("type", 0));

        int huodongshu = goodsService.selectCount(new EntityWrapper<Goods>()
                .eq("leagueid", leagueid).gt("type", 0));

        int mchs = storeService.selectCount(new EntityWrapper<Store>()
                .eq("leagueid", leagueid));

        //===待完善***
//        StoreStat storeStat = storeStatMapper.selectById(sid);
        StoreStat storeStat = new StoreStat();

        storeStat.setKeyong(saccount.getAmount());
        storeStat.setDairuzhang(saccount.getFanliing());
        storeStat.setUsers(ucout == null ? 0 : ucout.getGoodsNumber());
        storeStat.setOrders(ddshu);
        storeStat.setHuodongs(huodongshu);
        storeStat.setGoods(shangpinshu);
        storeStat.setMchs(mchs);

        Map<String, SaccountFlow> stat = new HashMap<String, SaccountFlow>();
        //===今日统计
        int today = Integer.parseInt(DateUtil.getToday("YYYYMMdd"));
        SaccountFlow tianstat = saccountFlowService.selectOne(new EntityWrapper<SaccountFlow>()
                .setSqlSelect("sid,sum(amount) amount,sum(total) total,sum(youhui) youhui,sum(mamount) mamount,sum(mamount) mamount,sum(1) jifen")
                .eq("leagueid", leagueid)
                .eq("createday", today)
                .ne("xftype", XftypeEnum.ruzhang.getVal())
                .ne("xftype", XftypeEnum.refund.getVal())
                .groupBy("sid")
        );
        //===待完善***退款流水待完善
        SaccountFlow tiantui = saccountFlowService.selectOne(new EntityWrapper<SaccountFlow>()
                .setSqlSelect("sum(amount) amount")
                .eq("leagueid", leagueid)
                .eq("createday", today)
                .eq("xftype", XftypeEnum.refund.getVal())
                .groupBy("sid")
        );
        int tiandd = saccountFlowService.selectCount(new EntityWrapper<SaccountFlow>()
                .setSqlSelect("1")
                .eq("leagueid", leagueid)
                .eq("iotype", 1)
                .eq("createday", today)
                .ne("xftype", XftypeEnum.ruzhang.getVal())
        );
        if (tianstat == null) tianstat = new SaccountFlow();
        tianstat.setTuikuan(tiantui == null ? zero : tiantui.getAmount());
        tianstat.setOrders(tiandd);
        stat.put("tian", tianstat);

        //===周统计
        int zhou0 = Integer.parseInt(DateUtil.addToday(-6, "YYYYMMdd"));
        int zhou1 = Integer.parseInt(DateUtil.getToday("YYYYMMdd"));
        SaccountFlow zhoustat = saccountFlowService.selectOne(new EntityWrapper<SaccountFlow>()
                .setSqlSelect("sid,sum(amount) amount,sum(total) total,sum(youhui) youhui,sum(mamount) mamount,sum(mamount) mamount,sum(1) jifen")
                .eq("leagueid", leagueid)
                .ge("createday", zhou0)
                .le("createday", zhou1)
                .ne("xftype", XftypeEnum.ruzhang.getVal())
                .ne("xftype", XftypeEnum.refund.getVal())
                .groupBy("sid")
        );

        SaccountFlow zhoutui = saccountFlowService.selectOne(new EntityWrapper<SaccountFlow>()
                .setSqlSelect("sum(amount) amount")
                .eq("leagueid", leagueid)
                .ge("createday", zhou0)
                .le("createday", zhou1)
                .eq("xftype", XftypeEnum.refund.getVal())
        );
        int zhoudd = saccountFlowService.selectCount(new EntityWrapper<SaccountFlow>()
                .setSqlSelect("1")
                .eq("leagueid", leagueid)
                .eq("iotype", 1)
                .ge("createday", zhou0)
                .le("createday", zhou1)
                .ne("xftype", XftypeEnum.ruzhang.getVal())
        );
        if (zhoustat == null) zhoustat = new SaccountFlow();
        zhoustat.setTuikuan(zhoutui == null ? zero : zhoutui.getAmount());
        zhoustat.setOrders(zhoudd);
        stat.put("zhou", zhoustat);

        //===月统计
        int yue0 = Integer.parseInt(DateUtil.addToday(-29, "YYYYMMdd"));
        int yue1 = Integer.parseInt(DateUtil.getToday("YYYYMMdd"));
        SaccountFlow yuestat = saccountFlowService.selectOne(new EntityWrapper<SaccountFlow>()
                .setSqlSelect("sid,sum(amount) amount,sum(total) total,sum(youhui) youhui,sum(mamount) mamount,sum(mamount) mamount,sum(1) jifen")
                .eq("leagueid", leagueid)
                .ge("createday", yue0)
                .le("createday", yue1)
                .ne("xftype", XftypeEnum.ruzhang.getVal())
                .ne("xftype", XftypeEnum.refund.getVal())
                .groupBy("sid")
        );
        SaccountFlow yuetui = saccountFlowService.selectOne(new EntityWrapper<SaccountFlow>()
                .setSqlSelect("sum(amount) amount")
                .eq("leagueid", leagueid)
                .ge("createday", yue0)
                .le("createday", yue1)
                .eq("xftype", XftypeEnum.refund.getVal())
        );
        int yuedd = saccountFlowService.selectCount(new EntityWrapper<SaccountFlow>()
                .setSqlSelect("1")
                .eq("leagueid", leagueid)
                .eq("iotype", 1)
                .ge("createday", zhou0)
                .le("createday", zhou1)
                .ne("xftype", XftypeEnum.ruzhang.getVal())
        );
        if (yuestat == null) yuestat = new SaccountFlow();
        yuestat.setTuikuan(yuetui == null ? zero : yuetui.getAmount());
        yuestat.setOrders(yuedd);
        stat.put("yue", yuestat);

        //联盟统计
        json.setInfo(storeStat);
        json.setExtra(stat);

        return json;
    }
}
